package com.songtech.uitest.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.TestNG;
import org.testng.xml.Parser;
import org.testng.xml.XmlSuite;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;



public class MainClass {


    public static void main(String[] args){

        TestNG tng = new TestNG();

        ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring-test-config.xml");
        JarPathHelper helper = (JarPathHelper) context.getBean("jarPathHelper");
        InputStream in = helper.getRootPathStreamInJar("testng.xml");

        Parser parser = new Parser(in);
        List<XmlSuite> suites = new ArrayList<XmlSuite>();

        try{
            suites = parser.parseToList();

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        tng.setXmlSuites(suites);
        tng.run();


        //注意问题！！！在这里想获取在testng执行时的BrowserDriver实例，但实际获取不到！
        //因为这里的context不是testng运行时获取的context!这是另一个context了！
        //目前还没想到如何能获取testNG运行时的spring上下文context
        //替代的办法是在测试用例目录下新写了一个TestBeforeAndAfter空用例，专门负责铺垫和收尾的工作。
//        try {
//            BrowserDriver bd = (BrowserDriver) context.getBean("browserDriver");
//            bd.closeAllDriver();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }



        //发邮件
        ZipUtils zipUtils = (ZipUtils) context.getBean("zipUtils");
        SendMail sendMail = (SendMail) context.getBean("sendMail");

        try {
            zipUtils.resultsCompress();
            System.out.println("测试结果压缩完毕");
            sendMail.sendMailComplex();
            System.out.println("邮件发送完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
